﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SmartSpace.Silverlight
{
    public partial class SlPagedbar : UserControl
    {
        private int m_totalCount;

        public int CurrentPageIndex { get; private set; }
        public int PageCount { get; private set; }
        public int Limit { get; private set; }

        public int Start
        {
            get
            {
                return CurrentPageIndex* Limit;
            }
        }

        public int TotalCount
        {
            get
            {
                return m_totalCount;
            }
        }

        public event Action<int> PageIndexChanged;

        public SlPagedbar()
        {
            InitializeComponent();
            CurrentPageIndex = 0;
            PageCount = 0;

            Update(0, 25, 0);
        }

        private void m_buttonFirst_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentPageIndex == 0)
            {
                return;
            }

            CurrentPageIndex = 0;

            if (PageIndexChanged != null)
            {
                PageIndexChanged(CurrentPageIndex);
            }
        }

        private void m_buttonPrev_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentPageIndex == 0)
            {
                return;
            }

            CurrentPageIndex--;

            if (PageIndexChanged != null)
            {
                PageIndexChanged(CurrentPageIndex);
            }
        }

        private void m_buttonNext_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentPageIndex == PageCount)
            {
                return;
            }

            CurrentPageIndex++;

            if (PageIndexChanged != null)
            {
                PageIndexChanged(CurrentPageIndex);
            }
        }

        private void m_buttonLast_Click(object sender, RoutedEventArgs e)
        {
            if (CurrentPageIndex == PageCount - 1)
            {
                return;
            }

            CurrentPageIndex = PageCount - 1;

            if (PageIndexChanged != null)
            {
                PageIndexChanged(CurrentPageIndex);
            }
        }

        private void m_textBoxPageIndex_TextChanged(object sender, TextChangedEventArgs e)
        {
            int newIndex = CurrentPageIndex;

            try
            {
                newIndex = Convert.ToInt32(m_textBoxPageIndex.Text);
                newIndex--;
                if (newIndex < 0)
                {
                    newIndex = 0;
                }
                else if (newIndex > PageCount - 1)
                {
                    newIndex = PageCount - 1;
                }
            }
            catch
            {
                newIndex = CurrentPageIndex;
            }
            if(newIndex != CurrentPageIndex)
            {
                if (newIndex < 0)
                {
                    newIndex = 0;
                }
                else if (newIndex > PageCount - 1)
                {
                    newIndex = PageCount - 1;
                }

                CurrentPageIndex = newIndex;

                if (PageIndexChanged != null)
                {
                    PageIndexChanged(CurrentPageIndex);
                }
            }

        }

        public void Update(int start, int limit, int totalCount)
        {
            m_totalCount = totalCount;

            Limit = limit;
            if (limit == 0)
            {
                PageCount = 0;
                CurrentPageIndex = 0;
            }
            else
            {
                PageCount = totalCount / limit;
                if (totalCount % limit != 0)
                {
                    PageCount++;
                }

                CurrentPageIndex = start / limit;
                if (start % limit != 0)
                {
                    CurrentPageIndex++;
                }
            }

            m_textBoxPageIndex.Text = (CurrentPageIndex + 1).ToString();
            m_textTotalCount.Text = "一共有" + totalCount.ToString() + "条记录";
            if (PageCount == 0)
            {
                m_textTotalPage.Text = "/1页";
            }
            else
            {
                m_textTotalPage.Text = "/" + PageCount.ToString() + "页";
            }

            if (CurrentPageIndex <= 0)
            {
                m_buttonFirst.IsEnabled = false;
                m_buttonPrev.IsEnabled = false;
            }
            else
            {
                m_buttonFirst.IsEnabled = true;
                m_buttonPrev.IsEnabled = true;
            }

            if (CurrentPageIndex >= PageCount - 1)
            {
                m_buttonLast.IsEnabled = false;
                m_buttonNext.IsEnabled = false;
            }
            else
            {
                m_buttonLast.IsEnabled = true;
                m_buttonNext.IsEnabled = true;
            }
        }
    }
}
